package com.sanley.coronavirus.dao;

import com.sanley.coronavirus.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {
    //通过username查找User
    @Select("select * from user where username=#{username}")
    @Results({@Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "unit", column = "unit"),
            @Result(property = "authenticationList", column = "id", javaType = java.util.List.class, many = @Many(select = "com.sanley.coronavirus.dao.AuthenticationDao.findByUserId"))})
    public User findByUsername(String username);

    //添加用户
    @Insert("insert into user(username,password,phone,unit,name)values(#{username},#{password},#{phone},#{unit},#{name})")
    public void addUser(User user);

    @Insert("insert into user(username,password,phone,unit,name,baseId)values(#{username},#{password},#{phone},#{unit},#{name},#{baseId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int addOneUser(User user);

    //查找所有用户
    @Select("<script>select id,username,name,phone,unit from user <if test='keyword!=null'>where name like concat('%',#{keyword},'%')</if> </script>")
    @Results({@Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "unit", column = "unit"),
            @Result(property = "authenticationList", column = "id", javaType = List.class, many = @Many(select = "com.sanley.coronavirus.dao.AuthenticationDao.findByUserId"))})
    public List<User> findAll(String keyword);

    //查找用户名字
    @Select("select name from user where username=#{username}")
    String selectNameByUsername(String username);

    //删除用户
    @Delete("delete from user where id=#{id}")
    public void deleteUser(int id);

    //为账号添加用户权限
    @Insert("insert into user_authorities(authentication_id,user_id)values(2,#{user_id})")
    public void addUserRole(int id);

    //为账号添加超级管理者权限
    @Insert("insert into user_authorities(authentication_id,user_id)values(1,#{user_id})")
    public void addAdminRole(int id);

    @Select("select b.id from base as b join user as u on u.baseId=b.id where u.username=#{username} ")
    int getBaseIdByUsername(String username);

    @Select("select id,username,name,phone,unit from user where username=#{username}")
    @Results({@Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "unit", column = "unit")})
    User selectUserInfoByUsername(String username);
}
